<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('指标同步') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('机构名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('机构名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('机构schema')" prop="dataSchema">
        <el-input
          v-model="queryParams.dataSchema"
          :placeholder="$t('机构schema')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="wordOnlineKeySyncRecordList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('机构名称')" align="center" prop="name" />
      <el-table-column :label="$t('机构schema')" align="center" prop="dataSchema" />
      <el-table-column :label="$t('环境')" align="center" prop="env" />
      <el-table-column :label="$t('推送状态')" align="center" prop="pushStatus" :formatter="formatStatus" />
      <el-table-column :label="$t('失败原因')" show-overflow-tooltip align="center" prop="failureReason" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:word:edit']"
            size="small"
            type="text"
            icon="el-icon-view"
            @click="updateHandle(scope.row)"
          >{{ $t('查看') }}</el-button>
          <el-button
            v-if="scope.row.pushStatus !== '1'"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="publishHandle(scope.row)"
          >{{ $t('发布') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="searchList"
    /> -->

    <!-- 添加或修改指标同步对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="" append-to-body>
      <wordOnlineKeySyncRecordForm ref="wordOnlineKeySyncRecordForm" />
    </el-dialog>
  </div>
</template>

<script>
import { operateInfo } from '@/api/system/word/keyword/wordOnlineKeyWord'
import { listWordOnlineKeySyncRecord, initKeySyncRecord } from '@/api/system/word/wordOnlineKeySyncRecord'
import wordOnlineKeySyncRecordForm from './wordOnlineKeySyncRecordForm.vue'
export default {
    components: {
      wordOnlineKeySyncRecordForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 指标同步表格数据
          wordOnlineKeySyncRecordList: [],
          envInfo: process.env.NODE_ENV,
          // 弹出层标题
          title: '指标同步',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              keyId: undefined,
              name: undefined,
              dataSchema: undefined,
              env: undefined,
              pushStatus: undefined,
              failureReason: undefined,
              pageNum: 1,
              pageSize: 100
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      // this.getList()
  },
  methods: {
    init(data) {
      this.getList(data)
    },
    /** 查询指标同步列表 */
    getList(data) {
        this.loading = true
        if (data) {
          this.queryParams.name = undefined
          this.queryParams.dataSchema = undefined
          const queryObj = {
            keyId: data.id
          }
          this.queryParams.keyId = data.id
          initKeySyncRecord(queryObj).then(response => {
              this.wordOnlineKeySyncRecordList = response.rows
              this.total = response.total
              this.loading = false
          }).catch(() => {
              this.loading = false
          })
        } else {
          this.loading = false
        }
    },
    searchList() {
        this.loading = true
        listWordOnlineKeySyncRecord(this.queryParams).then(response => {
            this.wordOnlineKeySyncRecordList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            keyId: undefined,
            name: undefined,
            dataSchema: undefined,
            env: undefined,
            pushStatus: '0',
            failureReason: undefined,
            remark: undefined,
            createBy: undefined,
            updateBy: undefined,
            createTime: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.searchList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.wordOnlineKeySyncRecordForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.wordOnlineKeySyncRecordForm.init(row)
        })
    },
    formatStatus(row) {
      if (row.pushStatus === '0') {
        return '未发布'
      } else if (row.pushStatus === '1') {
        return '已发布'
      } else if (row.pushStatus === '2') {
        return '发布失败'
      }
    },
    publishHandle(row) {
      this.$prompt('请输入推送秘钥', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputErrorMessage: '秘钥不能为空',
          inputValidator: (value) => {
            if (!value.trim()) {
              return '秘钥不能为空'
            }
            return true
          }
        }).then(({ value }) => {
          return operateInfo(row.keyId, value, row.dataSchema, row.id)
        }).then(() => {
            this.searchList()
            this.msgInfo(this.$t('发布完成，请查看结果'))
        }).catch(() => {
            this.loading = false
        })
    }
  }
}
</script>
